using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace NetworkTimer
{
    public partial class frmBroadBandScheduler : Form
    {
        public frmBroadBandScheduler()
        {
            InitializeComponent();
        }
        enum ToggleMode
        {
            /// <summary>
            /// If the connection is enabled, it will be disabled, and vice versa.
            /// </summary>
            Toggle,
            /// <summary>
            /// If the connection is disabled, it will be enabled; otherwise nothing is done.
            /// </summary>
            Enable,
            /// <summary>
            /// If the connection is enabled, it will be disabled; otherwise nothing is done.
            /// </summary>
            Disable
        }
        private void disableCnt()
        {
            dtpStart.Enabled = false;
            dtpStop.Enabled = false;
            chkStp.Enabled = false;
            button1.Enabled = false;
            button2.Enabled = true;

        }
        private void enableCnt()
        {
            dtpStart.Enabled = true;
            dtpStop.Enabled = true;
            chkStp.Enabled = true;
            button1.Enabled = true;
            button2.Enabled = false;

        }

        //shutdown computer
        private void Shutdown()
        {
            try
            {
                ManagementBaseObject mboShutdown = null;
                ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
                mcWin32.Get();

                // You can't shutdown without security privileges
                mcWin32.Scope.Options.EnablePrivileges = true;
                ManagementBaseObject mboShutdownParams =
                         mcWin32.GetMethodParameters("Win32Shutdown");

                // Flag 1 means we want to shut down the system. Use "2" to reboot.
                mboShutdownParams["Flags"] = "1";
                mboShutdownParams["Reserved"] = "0";
                foreach (ManagementObject manObj in mcWin32.GetInstances())
                {
                    mboShutdown = manObj.InvokeMethod("Win32Shutdown",
                                                   mboShutdownParams, null);
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (chkApp.Checked == true)
            {
                if (string.IsNullOrEmpty(txtPath.Text))
                {
                    MessageBox.Show("Please select an Application...", "BroadBand Scheduler", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }

            lbMsg.Text = "";
            tmrStart.Enabled = true;
            tmrStart.Start();
            

            if (chkStp.Checked == true)
            {
                tmrStop.Enabled = true;
                tmrStop.Start();
            }
            disableCnt();

            SerializeSettings();
            
        }


        /// <summary>
        /// Gets the Network Connections folder in the control panel.
        /// </summary>
        /// <returns>The Folder for the Network Connections folder, or null if it was not found.</returns>
        static Shell32.Folder GetNetworkConnectionsFolder()
        {
            Shell32.Shell sh = new Shell32.Shell();
            Shell32.Folder controlPanel = sh.NameSpace(3); // Control panel

            Shell32.FolderItems items = controlPanel.Items();
            foreach (Shell32.FolderItem item in items)
            {
                if (item.Name == Properties.Settings.Default.NetworkConnectionsFolderName)
                    return (Shell32.Folder)item.GetFolder;
            }

            return null;
        }

        /// <summary>
        /// Gets the network connection with the specified name from the specified shell folder.
        /// </summary>
        /// <param name="networkConnectionsFolder">The Network Connections folder.</param>
        /// <param name="connectionName">The name of the network connection.</param>
        /// <returns>The FolderItem for the network connection, or null if it was not found.</returns>
        static Shell32.FolderItem2 GetNetworkConnection(Shell32.Folder networkConnectionsFolder, string connectionName)
        {
            Shell32.FolderItems items = networkConnectionsFolder.Items();
            foreach (Shell32.FolderItem2 item in items)
            {
                if (item.Name == connectionName)
                {
                    return item;
                }
            }

            return null;
        }

        /// <summary>
        /// Gets whether or not the network connection is enabled and the command to enable/disable it.
        /// </summary>
        /// <param name="networkConnection">The network connection to check.</param>
        /// <param name="enableDisableVerb">On return, receives the verb used to enable or disable the connection.</param>
        /// <returns>True if the connection is enabled, false if it is disabled.</returns>
        static bool IsNetworkConnectionEnabled(Shell32.FolderItem2 networkConnection, out Shell32.FolderItemVerb enableDisableVerb)
        {
            Shell32.FolderItemVerbs verbs = networkConnection.Verbs();
            foreach (Shell32.FolderItemVerb verb in verbs)
            {
                if (verb.Name == Properties.Settings.Default.EnableVerbName)
                {
                    enableDisableVerb = verb;
                    return false;
                }
                else if (verb.Name == Properties.Settings.Default.DisableVerbName)
                {
                    enableDisableVerb = verb;
                    return true;
                }
            }

            throw new ArgumentException("No enable or disable verb found.");
        }



        private void tmrStart_Tick(object sender, EventArgs e)
        {
            string StartTime = dtpStart.Value.ToString("hh:mmtt");
            string TimeNow = DateTime.Now.ToString("hh:mmtt");

            if (StartTime == TimeNow)
            {

                string[] args = new string[2];
                args[0] = Properties.Settings.Default.NetworkConnetionName;
                args[1] = "enable";
                if (args.Length < 1)
                {
                    lbMsg.Text = "Please check the configuration settings...";
                    //stop the timer
                    tmrStart.Stop();
                    tmrStart.Enabled = false;
                    if (chkStp.Checked != true)
                    {
                        enableCnt();
                    }
                    return;
                }

                ToggleMode mode;
                string connectionName = args[0];
                if (args.Length == 1)
                    mode = ToggleMode.Toggle;
                else if (args.Length == 2)
                {
                    if (args[1] == "enable")
                        mode = ToggleMode.Enable;
                    else if (args[1] == "disable")
                        mode = ToggleMode.Disable;
                    else
                    {
                        //PrintUsage();
                        lbMsg.Text = "Please check the configuration settings...";
                        //stop the timer
                        tmrStart.Stop();
                        tmrStart.Enabled = false;
                        if (chkStp.Checked != true)
                        {
                            enableCnt();
                        }
                        return;
                    }
                }
                else
                {
                    //PrintUsage();
                    lbMsg.Text = "Please check the configuration settings...";
                    //stop the timer
                    tmrStart.Stop();
                    tmrStart.Enabled = false;
                    if (chkStp.Checked != true)
                    {
                        enableCnt();
                    }
                    return;
                }

                Shell32.Folder networkConnectionsFolder = GetNetworkConnectionsFolder();
                if (networkConnectionsFolder == null)
                {
                    lbMsg.Text = "Network connections folder not found.";
                    //stop the timer
                    tmrStart.Stop();
                    tmrStart.Enabled = false;
                    if (chkStp.Checked != true)
                    {
                        enableCnt();
                    }
                    return;
                }

                Shell32.FolderItem2 networkConnection = GetNetworkConnection(networkConnectionsFolder, connectionName);
                
                if (networkConnection == null)
                {
                    lbMsg.Text = "Network connection not found.";
                    //stop the timer
                    tmrStart.Stop();
                    tmrStart.Enabled = false;
                    if (chkStp.Checked != true)
                    {
                        enableCnt();
                    }
                    return;
                }

                Shell32.FolderItemVerb verb;
                try
                {
                    if (IsNetworkConnectionEnabled(networkConnection, out verb))
                    {
                        if (mode == ToggleMode.Enable)
                        {
                            lbMsg.Text = "Connection is already enabled.";

                            //Load a Application
                            try
                            {
                                if (chkApp.Checked == true)
                                {
                                    if (!string.IsNullOrEmpty(txtPath.Text))
                                    {
                                        System.Diagnostics.Process proc = new System.Diagnostics.Process();
                                        proc.EnableRaisingEvents = false;
                                        proc.StartInfo.FileName = txtPath.Text.Trim();
                                        proc.Start();
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                        else
                        {
                            lbMsg.Text = "Disabling connection...";
                            verb.DoIt();
                            System.Threading.Thread.Sleep(2000);
                            lbMsg.Text += "OK";

                            //shutdown computer
                            if (chkShutDownPP.Checked == true)
                            {
                                Org.Mentalis.Utilities.WindowsController.ExitWindows(Org.Mentalis.Utilities.RestartOptions.PowerOff, true);
                            }

                        }
                    }
                    else
                    {
                        if (mode == ToggleMode.Disable)
                            lbMsg.Text = "Connections is already disabled.";
                        else
                        {
                            lbMsg.Text = "Enabling connection...";
                            verb.DoIt();
                            System.Threading.Thread.Sleep(2000);
                            lbMsg.Text += "OK";

                            //Load a Application
                            try
                            {
                                if (chkApp.Checked == true)
                                {
                                    if (!string.IsNullOrEmpty(txtPath.Text))
                                    {
                                        System.Diagnostics.Process proc = new System.Diagnostics.Process();
                                        proc.EnableRaisingEvents = false;
                                        proc.StartInfo.FileName = txtPath.Text.Trim();
                                        proc.Start();
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                    }
                    //stop the timer
                    tmrStart.Stop();
                    tmrStart.Enabled = false;
                    if (chkStp.Checked != true)
                    {
                        enableCnt();
                    }

                }
                catch (ArgumentException ex)
                {
                    lbMsg.Text = ex.Message;
                    //stop the timer
                    tmrStart.Stop();
                    tmrStart.Enabled = false;
                    if (chkStp.Checked != true)
                    {
                        enableCnt();
                    }
                }
            }
        }

        private void tmrStop_Tick(object sender, EventArgs e)
        {
            string StopTime = dtpStop.Value.ToString("hh:mmtt");
            string TimeNow = DateTime.Now.ToString("hh:mmtt");

            if (StopTime == TimeNow)
            {

                string[] args = new string[2];
                args[0] = Properties.Settings.Default.NetworkConnetionName;
                args[1] = "disable";
                if (args.Length < 1)
                {
                    lbMsg.Text = "Please check the configuration settings...";
                    //stop the timer
                    tmrStop.Stop();
                    tmrStop.Enabled = false;
                    enableCnt();
                    return;
                }

                ToggleMode mode;
                string connectionName = args[0];
                if (args.Length == 1)
                    mode = ToggleMode.Toggle;
                else if (args.Length == 2)
                {
                    if (args[1] == "enable")
                        mode = ToggleMode.Enable;
                    else if (args[1] == "disable")
                        mode = ToggleMode.Disable;
                    else
                    {
                        //PrintUsage();
                        lbMsg.Text = "Please check the configuration settings...";
                        //stop the timer
                        tmrStop.Stop();
                        tmrStop.Enabled = false;
                        enableCnt();
                        return;
                    }
                }
                else
                {
                    //PrintUsage();
                    lbMsg.Text = "Please check the configuration settings...";
                    //stop the timer
                    tmrStop.Stop();
                    tmrStop.Enabled = false;
                    enableCnt();
                    return;
                }

                Shell32.Folder networkConnectionsFolder = GetNetworkConnectionsFolder();
                if (networkConnectionsFolder == null)
                {
                    lbMsg.Text = "Network connections folder not found.";
                    //stop the timer
                    tmrStop.Stop();
                    tmrStop.Enabled = false;
                    enableCnt();
                    return;
                }

                Shell32.FolderItem2 networkConnection = GetNetworkConnection(networkConnectionsFolder, connectionName);
                if (networkConnection == null)
                {
                    lbMsg.Text = "Network connection not found.";
                    //stop the timer
                    tmrStop.Stop();
                    tmrStop.Enabled = false;
                    enableCnt();
                    return;
                }

                Shell32.FolderItemVerb verb;
                try
                {
                    if (IsNetworkConnectionEnabled(networkConnection, out verb))
                    {
                        if (mode == ToggleMode.Enable)
                            lbMsg.Text = "Connection is already enabled.";
                        else
                        {
                            lbMsg.Text = "Disabling connection...";
                            verb.DoIt();
                            System.Threading.Thread.Sleep(2000);
                            lbMsg.Text += "OK";

                            //shutdown computer
                            if (chkShutDownPP.Checked == true)
                            {
                                Org.Mentalis.Utilities.WindowsController.ExitWindows(Org.Mentalis.Utilities.RestartOptions.PowerOff, true);
                            }
                        }
                    }
                    else
                    {
                        if (mode == ToggleMode.Disable)
                            lbMsg.Text = "Connections is already disabled.";
                        else
                        {
                            lbMsg.Text = "Enabling connection...";
                            verb.DoIt();
                            System.Threading.Thread.Sleep(2000);
                            lbMsg.Text += "OK";
                        }
                    }
                    //stop the timer
                    tmrStop.Stop();
                    tmrStop.Enabled = false;
                    enableCnt();

                }
                catch (ArgumentException ex)
                {
                    lbMsg.Text = ex.Message;
                    //stop the timer
                    tmrStop.Stop();
                    tmrStop.Enabled = false;
                    enableCnt();
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            enableCnt();
            tmrStart.Stop();
            tmrStop.Stop();
            tmrStart.Enabled = false;
            tmrStop.Enabled = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (tmrStart.Enabled == true)
            {
                e.Cancel = true;
                MessageBox.Show("Process is going on....", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (tmrStop.Enabled == true)
            {
                e.Cancel = true;
                MessageBox.Show("Process is going on....", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            //for Dail-up Connection
            if (tmrDPStart.Enabled == true)
            {
                e.Cancel = true;
                MessageBox.Show("Process is going on....", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (tmrDPStop.Enabled == true)
            {
                e.Cancel = true;
                MessageBox.Show("Process is going on....", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button2.Enabled = false;
            btnDPStop.Enabled = false;
            DeserializeSettings();
        }

        private void chkStp_CheckedChanged(object sender, EventArgs e)
        {
            if (chkStp.Checked == true)
            {
                dtpStop.Enabled = true;
            }
            else
            {
                dtpStop.Enabled = false;
            }           
        }

        private void button3_Click(object sender, EventArgs e)
        {
            opDlg.Filter = "Executable|*.exe";
            if (opDlg.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = opDlg.FileName;
            }
        }

        private void chkApp_CheckedChanged(object sender, EventArgs e)
        {
            if (chkApp.Checked == true)
            {
                txtPath.Enabled = true;
                btnBrws.Enabled = true;
            }
            else
            {
                txtPath.Enabled = false;
                btnBrws.Enabled = false;
                txtPath.Text = "";
            }
        }

        // For dial-up Connection

        private void disableDPCnt()
        {
            dtpDPStart.Enabled = false;
            dtpDPStop.Enabled = false;
            chkDPEnable.Enabled = false;
            btnDPStart.Enabled = false;
            btnDPStop.Enabled = true;

        }
        private void enableDPCnt()
        {
            dtpDPStart.Enabled = true;
            dtpDPStop.Enabled = true;
            chkDPEnable.Enabled = true;
            btnDPStart.Enabled = true;
            btnDPStop.Enabled = false;

        }

        

        /// <summary>
        /// Gets whether or not the DialUp connection is enabled and the command to enable/disable it.
        /// </summary>
        /// <param name="networkConnection">The network connection to check.</param>
        /// <param name="enableDisableVerb">On return, receives the verb used to enable or disable the connection.</param>
        /// <returns>True if the connection is enabled, false if it is disabled.</returns>
        static bool IsDailUpConnectionEnabled(Shell32.FolderItem2 networkConnection, out Shell32.FolderItemVerb enableDisableVerb)
        {
            Shell32.FolderItemVerbs verbs = networkConnection.Verbs();
            foreach (Shell32.FolderItemVerb verb in verbs)
            {
                if (verb.Name == Properties.Settings.Default.DailUpConnectEnable)
                {
                    enableDisableVerb = verb;
                    return false;
                }
                else if (verb.Name == Properties.Settings.Default.DailUpConnectDisable)
                {
                    enableDisableVerb = verb;
                    return true;
                }
            }

            throw new ArgumentException("No enable or disable verb found.");
        }


        private void btnDPStart_Click(object sender, EventArgs e)
        {
            if (chkApp.Checked == true)
            {
                if (string.IsNullOrEmpty(txtPath.Text))
                {
                    MessageBox.Show("Please select an Application...", "BroadBand Scheduler", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }

            lbMsgDP.Text = "";
            tmrDPStart.Enabled = true;
            tmrDPStart.Start();


            if (chkDPEnable.Checked == true)
            {
                tmrDPStop.Enabled = true;
                tmrDPStop.Start();
            }
            disableDPCnt();

            SerializeSettings();
            
        }


        //Serialize the object to a Bridge Settings
        private void SerializeSettings()
        {
            
            MySetting objset = new MySetting();
            objset.BridgeStartTime = dtpDPStart.Value;
            objset.BridgeStopTime = dtpDPStop.Value;
            objset.BridgeStopEnable = chkDPEnable.Checked;
            objset.BridgeShutdown = chkShutDown.Checked;


            objset.PPPoEStartTime = dtpStart.Value;
            objset.PPPoEStopTime = dtpStop.Value;
            objset.PPPoEStopEnable = chkStp.Checked;
            objset.PPPoEShutdown = chkShutDownPP.Checked;

            objset.AppEnable = chkApp.Checked;
            objset.AppPath = txtPath.Text;


            Stream stream = File.Open(Application.StartupPath + "//MyAppSetings.osl", FileMode.Create);
            BinaryFormatter bformatter = new BinaryFormatter();

            bformatter.Serialize(stream, objset);
            stream.Close();
        }

        //Deserialize the object to a Bridge Settings
        private void DeserializeSettings()
        {            
            if (File.Exists("MyAppSetings.osl") == true)
            {
                MySetting objSet = new MySetting();

                Stream stream = File.Open(Application.StartupPath + "//MyAppSetings.osl", FileMode.Open);
                BinaryFormatter bformatter = new BinaryFormatter();

                objSet = (MySetting)bformatter.Deserialize(stream);
                stream.Close();

                dtpDPStart.Value = objSet.BridgeStartTime;
                dtpDPStop.Value = objSet.BridgeStopTime;
                chkDPEnable.Checked = objSet.BridgeStopEnable;
                chkShutDown.Checked = objSet.BridgeShutdown;

                dtpStart.Value = objSet.PPPoEStartTime;
                dtpStop.Value = objSet.PPPoEStopTime;
                chkStp.Checked = objSet.PPPoEStopEnable;
                chkShutDownPP.Checked = objSet.PPPoEShutdown;

                chkApp.Checked = objSet.AppEnable;
                txtPath.Text = objSet.AppPath;
            }
        }

        private void btnDPStop_Click(object sender, EventArgs e)
        {
            enableDPCnt();
            tmrDPStart.Stop();
            tmrDPStop.Stop();
            tmrDPStart.Enabled = false;
            tmrDPStop.Enabled = false;
        }

        private void chkDPEnable_CheckedChanged(object sender, EventArgs e)
        {
            if (chkDPEnable.Checked == true)
            {
                dtpDPStop.Enabled = true;
            }
            else
            {
                dtpDPStop.Enabled = false;
            }    
        }

        private void tmrDPStart_Tick(object sender, EventArgs e)
        {
            string StartTime = dtpDPStart.Value.ToString("hh:mmtt");
            string TimeNow = DateTime.Now.ToString("hh:mmtt");

            if (StartTime == TimeNow)
            {

                string[] args = new string[2];
                args[0] = Properties.Settings.Default.DailUpConnection;
                args[1] = "enable";
                if (args.Length < 1)
                {
                    lbMsgDP.Text = "Please check the configuration settings...";
                    //stop the timer
                    tmrDPStart.Stop();
                    tmrDPStart.Enabled = false;
                    if (chkDPEnable.Checked != true)
                    {
                        enableDPCnt();
                    }
                    return;
                }

                ToggleMode mode;
                string connectionName = args[0];
                if (args.Length == 1)
                    mode = ToggleMode.Toggle;
                else if (args.Length == 2)
                {
                    if (args[1] == "enable")
                        mode = ToggleMode.Enable;
                    else if (args[1] == "disable")
                        mode = ToggleMode.Disable;
                    else
                    {
                        //PrintUsage();
                        lbMsgDP.Text = "Please check the configuration settings...";
                        //stop the timer
                        tmrDPStart.Stop();
                        tmrDPStart.Enabled = false;
                        if (chkDPEnable.Checked != true)
                        {
                            enableDPCnt();
                        }
                        return;
                    }
                }
                else
                {
                    //PrintUsage();
                    lbMsgDP.Text = "Please check the configuration settings...";
                    //stop the timer
                    tmrDPStart.Stop();
                    tmrDPStart.Enabled = false;
                    if (chkDPEnable.Checked != true)
                    {
                        enableDPCnt();
                    }
                    return;
                }

                Shell32.Folder networkConnectionsFolder = GetNetworkConnectionsFolder();
                if (networkConnectionsFolder == null)
                {
                    lbMsgDP.Text = "Network connections folder not found.";
                    //stop the timer
                    tmrDPStart.Stop();
                    tmrDPStart.Enabled = false;
                    if (chkDPEnable.Checked != true)
                    {
                        enableDPCnt();
                    }
                    return;
                }

                Shell32.FolderItem2 networkConnection = GetNetworkConnection(networkConnectionsFolder, connectionName);

                if (networkConnection == null)
                {
                    lbMsgDP.Text = "Network connection not found.";
                    //stop the timer
                    tmrDPStart.Stop();
                    tmrDPStart.Enabled = false;
                    if (chkDPEnable.Checked != true)
                    {
                        enableDPCnt();
                    }
                    return;
                }

                Shell32.FolderItemVerb verb;
                try
                {
                    if (IsDailUpConnectionEnabled(networkConnection, out verb))
                    {
                        if (mode == ToggleMode.Enable)
                        {
                            lbMsgDP.Text = "Connection is already enabled.";

                            //Load a Application
                            try
                            {
                                if (chkDPEnable.Checked == true)
                                {
                                    if (!string.IsNullOrEmpty(txtPath.Text))
                                    {
                                        System.Diagnostics.Process proc = new System.Diagnostics.Process();
                                        proc.EnableRaisingEvents = false;
                                        proc.StartInfo.FileName = txtPath.Text.Trim();
                                        proc.Start();
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                        else
                        {
                            lbMsgDP.Text = "Disabling connection...";
                            verb.DoIt();
                            System.Threading.Thread.Sleep(2000);
                            lbMsgDP.Text += "OK";

                            //shutdown computer
                            if (chkShutDown.Checked == true)
                            {
                                Org.Mentalis.Utilities.WindowsController.ExitWindows(Org.Mentalis.Utilities.RestartOptions.PowerOff, true);
                            }

                        }
                    }
                    else
                    {
                        if (mode == ToggleMode.Disable)
                            lbMsgDP.Text = "Connections is already disabled.";
                        else
                        {
                            lbMsgDP.Text = "Enabling connection...";
                            verb.DoIt();
                            System.Threading.Thread.Sleep(2000);
                            lbMsgDP.Text += "OK";

                            //Load a Application
                            try
                            {
                                if (chkApp.Checked == true)
                                {
                                    if (!string.IsNullOrEmpty(txtPath.Text))
                                    {
                                        System.Diagnostics.Process proc = new System.Diagnostics.Process();
                                        proc.EnableRaisingEvents = false;
                                        proc.StartInfo.FileName = txtPath.Text.Trim();
                                        proc.Start();
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                    }
                    //stop the timer
                    tmrDPStart.Stop();
                    tmrDPStart.Enabled = false;
                    if (chkDPEnable.Checked != true)
                    {
                        enableDPCnt();
                    }

                }
                catch (ArgumentException ex)
                {
                    lbMsgDP.Text = ex.Message;
                    //stop the timer
                    tmrDPStart.Stop();
                    tmrDPStart.Enabled = false;
                    if (chkDPEnable.Checked != true)
                    {
                        enableDPCnt();
                    }
                }
            }
        }

        private void tmrDPStop_Tick(object sender, EventArgs e)
        {
            string StopTime = dtpDPStop.Value.ToString("hh:mmtt");
            string TimeNow = DateTime.Now.ToString("hh:mmtt");

            if (StopTime == TimeNow)
            {

                string[] args = new string[2];
                args[0] = Properties.Settings.Default.DailUpConnection;
                args[1] = "disable";
                if (args.Length < 1)
                {
                    lbMsgDP.Text = "Please check the configuration settings...";
                    //stop the timer
                    tmrDPStop.Stop();
                    tmrDPStop.Enabled = false;
                    enableDPCnt();
                    return;
                }

                ToggleMode mode;
                string connectionName = args[0];
                if (args.Length == 1)
                    mode = ToggleMode.Toggle;
                else if (args.Length == 2)
                {
                    if (args[1] == "enable")
                        mode = ToggleMode.Enable;
                    else if (args[1] == "disable")
                        mode = ToggleMode.Disable;
                    else
                    {
                        //PrintUsage();
                        lbMsgDP.Text = "Please check the configuration settings...";
                        //stop the timer
                        tmrDPStop.Stop();
                        tmrDPStop.Enabled = false;
                        enableDPCnt();
                        return;
                    }
                }
                else
                {
                    //PrintUsage();
                    lbMsgDP.Text = "Please check the configuration settings...";
                    //stop the timer
                    tmrDPStop.Stop();
                    tmrDPStop.Enabled = false;
                    enableDPCnt();
                    return;
                }

                Shell32.Folder networkConnectionsFolder = GetNetworkConnectionsFolder();
                if (networkConnectionsFolder == null)
                {
                    lbMsgDP.Text = "Network connections folder not found.";
                    //stop the timer
                    tmrDPStop.Stop();
                    tmrDPStop.Enabled = false;
                    enableDPCnt();
                    return;
                }

                Shell32.FolderItem2 networkConnection = GetNetworkConnection(networkConnectionsFolder, connectionName);

                if (networkConnection == null)
                {
                    lbMsgDP.Text = "Network connection not found.";
                    //stop the timer
                    tmrDPStop.Stop();
                    tmrDPStop.Enabled = false;
                    enableDPCnt();
                    return;
                }

                Shell32.FolderItemVerb verb;
                try
                {
                    if (IsDailUpConnectionEnabled(networkConnection, out verb))
                    {
                        if (mode == ToggleMode.Enable)
                        {
                            lbMsgDP.Text = "Connection is already enabled.";

                            //Load a Application
                            try
                            {
                                if (chkDPEnable.Checked == true)
                                {
                                    if (!string.IsNullOrEmpty(txtPath.Text))
                                    {
                                        System.Diagnostics.Process proc = new System.Diagnostics.Process();
                                        proc.EnableRaisingEvents = false;
                                        proc.StartInfo.FileName = txtPath.Text.Trim();
                                        proc.Start();
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                        else
                        {
                            lbMsgDP.Text = "Disabling connection...";
                            verb.DoIt();
                            System.Threading.Thread.Sleep(2000);
                            lbMsgDP.Text += "OK";

                            //shutdown computer
                            if (chkShutDown.Checked == true)
                            {
                                Org.Mentalis.Utilities.WindowsController.ExitWindows(Org.Mentalis.Utilities.RestartOptions.PowerOff, true);
                            }

                        }
                    }
                    else
                    {
                        if (mode == ToggleMode.Disable)
                            lbMsgDP.Text = "Connections is already disabled.";
                        else
                        {
                            lbMsgDP.Text = "Enabling connection...";
                            verb.DoIt();
                            System.Threading.Thread.Sleep(2000);
                            lbMsgDP.Text += "OK";

                            //Load a Application
                            try
                            {
                                if (chkApp.Checked == true)
                                {
                                    if (!string.IsNullOrEmpty(txtPath.Text))
                                    {
                                        System.Diagnostics.Process proc = new System.Diagnostics.Process();
                                        proc.EnableRaisingEvents = false;
                                        proc.StartInfo.FileName = txtPath.Text.Trim();
                                        proc.Start();
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                    }
                    //stop the timer
                    tmrDPStop.Stop();
                    tmrDPStop.Enabled = false;
                    enableDPCnt();

                }
                catch (ArgumentException ex)
                {
                    lbMsgDP.Text = ex.Message;
                    //stop the timer
                    tmrDPStop.Stop();
                    tmrDPStop.Enabled = false;
                    enableDPCnt();
                }
            }
        }

      


        
        
    }
}